<?php
/**
 * Controller to manage access permissions for users' groups in the admin area
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Usermanagement Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class admin_PermissionsController extends Bel_Controller {
		
	/**
	 * Modules model
	 *
	 * @var Models_Modules
	 */
	private $_modules;	
	
	/**
	 * Groups model
	 *
	 * @var Models_Usermanagement_Groups
	 */
	private $_groups;	
	
	/**
	 * Initialization
	 *
	 */
	function init() {
		$this->_groups = new Models_Usermanagement_Groups ( );
		$this->_modules = new Models_Usermanagement_Modules();
		parent::init ();
		if (($messages = $this->_messages->getMessages ())) {
			$this->view->assign ( 'messages', $messages );
		}
	}
		
	/**
	 * Displays form with group's & module's dropdowns
	 *
	 */
	public function indexAction() {
		$groups = $this->_groups->getAll ();
		$modules = $this->_modules->getModules ();
		$form = new Bel_Forms_Builder ( 'permissions_form', '/admin/permissions/controllers/', 'GET' );
		
		$options = array ();
		foreach ( $groups as $group ) {
			$options [$group->user_group_id] = $group->user_group_name;
		}
		$form->addSubElement ( 'user_group_id', 'options', $options );
		
		$options = array ();
		foreach ( $modules as $module ) {
			$options [$module->module_id] = $module->module_name;
		}
		$form->addSubElement ( 'module_id', 'options', $options );
		
		unset ( $options );
		$this->view->assign ( 'form', $form );
		$this->view->display ( 'forms/default.tpl' );
	}	
	
	/**
	 * Displays controllers' list for module selected in indexAction
	 *
	 */
	public function controllersAction() {
		$group_id = $this->_request->getParam ( 'user_group_id' );
		$module_id = $this->_request->getParam ( 'module_id' );
		$controllers = $this->_modules->getControllersByModule ( $module_id );
		$this->view->assign ( 'user_group_id', $group_id );
		$this->view->assign ( 'module_id', $module_id );
		$this->view->assign ( 'controllers', $controllers );
		$this->view->display ( 'usermanagement/permissions/controllers.tpl' );
	}	
	
	/**
	 * Displays actions for controller selected in ctrlistAction
	 *
	 */
	public function actionsAction() {
		$request = $this->_request->getParams ();
		$actions = $this->_modules->getActionsByController ( $request ['controller_id'], $request ['user_group_id'] );
		
		$branch = $this->_modules->getParentBranch ( $request ['controller_id'] );
		unset ( $branch [0] );
		$this->view->assign ( 'actions', $actions );
		$this->view->assign ( 'user_group_id', $request ['user_group_id'] );
		$this->view->assign ( 'controller_id', $request ['controller_id'] );
		$this->view->display ( 'usermanagement/permissions/actions.tpl' ); //admin/permissions_form.tpl');
	}	
	
	/**
	 * Saves permissions to access actions from ctractionsAction for group selected in indexAction
	 *
	 */
	public function saveAction() {
		$request = $this->_request->getParams ();
		$permissions = new Models_Usermanagement_Permissions ( );
		$permissions->addPermissions ( $request ['user_group_id'], $request ['actions'], $request ['controller_id'] );
		
		Helpers_Usermanagement_Cache::cleanAclCacheForGroup($request ['user_group_id']);
		
		$this->_messages->setMessage ( 'Permissions saved' );		
		$this->_redirect ( '/admin/permissions/actions/controller_id/' . $request ['controller_id'] . '/user_group_id/' . $request ['user_group_id'] );
	
	}	
	
	/**
	 * Displays all action for module selected in indexAction
	 *
	 */
	public function allactionsAction() {
		$request = $this->_request->getParams ();
		$this->view->assign ( 'actions', $this->_modules->getModuleActions ( $request ['module_id'], $request ['user_group_id'] ) );
		$this->view->assign ( 'user_group_id', $request ['user_group_id'] );
		$this->view->assign ( 'module_id', $request ['module_id'] );
		$this->view->display ( 'usermanagement/permissions/allactions.tpl' ); //admin/all_permissions_form.tpl'
	}	
	
	/**
	 * Saves permissions to access action from allactionsAction for group selected in indexAction
	 *
	 */
	public function saveallAction() {
		$request = $this->_request->getParams ();
		$permissions = new Models_Usermanagement_Permissions ( );
		$permissions->addPermissions ( $request ['user_group_id'], $request ['actions'], $request ['module_id'] );
		
		Helpers_Usermanagement_Cache::cleanAclCacheForGroup($request ['user_group_id']);
				
		$this->_messages->setMessage ( 'Permissions saved' );
		$this->_redirect ( '/admin/permissions/allactions/module_id/' . $request ['module_id'] . '/user_group_id/' . $request ['user_group_id'] );
	}
}